package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.model.auto.ScoreItem;
import com.model.auto.ScoreLine;
import com.service.IScoreItemService;
import com.service.IScoreLineService;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 新高考/位次查询
 */
@RestController
@RequestMapping("/score/line")
public class ScoreLineController{

    @Autowired
    private IScoreLineService scoreLineService;
    @Autowired
    private IScoreItemService scoreItemService;

    /**
     * 获取分数线
     *
     * @param type 1：物理类 2：历史类
     * @param year 年份
     * @return
     */
    @GetMapping("/data")
    public R data(Integer type, String year){
        if(ObjectUtils.isEmpty(type) || ObjectUtils.isEmpty(year)){
            return R.error("请求参数不能为空。");
        }
        List<ScoreLine> list=scoreLineService.list(new LambdaQueryWrapper<ScoreLine>()
                .eq(ScoreLine::getType,type)
                .eq(ScoreLine::getYear,year)
        );

        if(ObjectUtils.isEmpty(list)){
            return R.error("未查询到分数线");
        }
        return R.ok(list.get(0));
    }

    /**
     * 获取所有分数人数列表
     * @param type
     * @param year
     * @return
     */
    @GetMapping("/item")
    public R item(Integer type, String year, Integer orderType){
        if(ObjectUtils.isEmpty(type) || ObjectUtils.isEmpty(year)){
            return R.error("请求参数不能为空。");
        }
        LambdaQueryWrapper<ScoreItem> queryWrapper = new LambdaQueryWrapper<ScoreItem>()
                .eq(ScoreItem::getType,type)
                .eq(ScoreItem::getYear,year);
        if (Objects.nonNull(orderType) && orderType == 2) {
            queryWrapper.orderByDesc(ScoreItem::getScore);
        } else {
            queryWrapper.orderByAsc(ScoreItem::getScore);
        }
        List<ScoreItem> list = scoreItemService.list(queryWrapper);

        if(ObjectUtils.isEmpty(list)){
            return R.error("未查询到分数人数列表");
        }
        return R.ok(list);
    }

    /**
     * 查询分数位次
     * @param score 分数
     * @return
     */
    @GetMapping("/getScore")
    public R getScore(Integer type, Integer score){
        if(ObjectUtils.isEmpty(type) || ObjectUtils.isEmpty(score)){
            return R.error("请求参数不能为空。");
        }
        List<ScoreItem> list=scoreItemService.list(new LambdaQueryWrapper<ScoreItem>()
                .eq(ScoreItem::getType,type)
                .eq(ScoreItem::getScore,score)
                .orderByDesc(ScoreItem::getYear)
        );

        if(ObjectUtils.isEmpty(list)){
            return R.error("未查询到分数位次信息");
        }
        return R.ok(list);
    }

    /**
     * 根据科目类型、分数获取位次
     * @param score 分数
     * @return
     */
    @GetMapping("/getPrecedence")
    public R getPrecedence(Integer type, Integer score){
        if(ObjectUtils.isEmpty(type) || ObjectUtils.isEmpty(score)){
            return R.error("请求参数不能为空。");
        }
        List<ScoreItem> list=scoreItemService.list(new LambdaQueryWrapper<ScoreItem>()
                .eq(ScoreItem::getType,type)
                .eq(ScoreItem::getScore,score)
                .orderByDesc(ScoreItem::getYear)
        );

        if(ObjectUtils.isEmpty(list)){
            return R.error("未查询到分数位次信息");
        }
        return R.ok("获取成功", list.get(0).getLowWeici());
    }

    /**
     * 根据科目类型、位次获取分数
     *
     * @param type       1：物理类 2：历史类
     * @param precedence 位次
     * @return
     */
    @GetMapping("/getScoreByParam")
    public R getScoreByParam(Integer type, Integer precedence){
        if(ObjectUtils.isEmpty(type) || ObjectUtils.isEmpty(precedence)){
            return R.error("请求参数不能为空。");
        }
        List<ScoreItem> list=scoreItemService.list(new LambdaQueryWrapper<ScoreItem>()
                .eq(ScoreItem::getType,type)
                //小于等于
                .le(ScoreItem::getTallWeici,precedence)
                //大于等于
                .ge(ScoreItem::getLowWeici,precedence)
                .orderByDesc(ScoreItem::getYear)
        );

        if(ObjectUtils.isEmpty(list)){
            return R.error("未查询到分数位次信息");
        }
        return R.ok("获取成功", list.get(0).getScore());
    }
}
